package com.ocom.webapi.controller;

import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.request.payconfig.SetConsDeviceMealConfigRequest;
import com.ocom.common.request.payconfig.UpdateConsDeviceInfoRequest;
import com.ocom.common.request.web.GetConsDeviceMealConfigRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.redis.util.RedisUtil;
import com.ocom.webapi.feign.PayConfigClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/2/6
 **/
@Slf4j
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class ConsDeviceMealConfigController {


	@Resource
	private RedisUtil redisUtil;

	@Autowired
	private PayConfigClientService payConfigClientService;

	@PostMapping(WebApiServiceUrl.Url_ConsumeDeviceConfig)
	public Result getConsDeviceMealConfig(@RequestBody GetConsDeviceMealConfigRequest request){
		return payConfigClientService.getConsDeviceMealConfig(request.getComId(),request.getDeviceNo(),request.getConsType());
	}

	@PostMapping(WebApiServiceUrl.Url_ConsumeDeviceUpdate)
	public Result setConsDeviceMealConfig(@RequestBody SetConsDeviceMealConfigRequest request){
		if(CommonUtil.isNull(request.getFaceAuth()) && request.getConfigList().size()>0){
			payConfigClientService.setConsDeviceMealConfig(request);
		}

		redisUtil.del("MqttXfDevice:" + request.getDeviceNo());

		UpdateConsDeviceInfoRequest request1 =new UpdateConsDeviceInfoRequest();
		request1.setCom_id(request.getComId());
		request1.setDevice_no(request.getDeviceNo());
		request1.setDevice_name(request.getDeviceName());
		request1.setPoint_id(request.getPointId());
		request1.setConsType(request.getConsType());
		request1.setDevice_mode(request.getDeviceMode());
		request1.setFaceAuth(request.getFaceAuth());
		request1.setFaceAuthTime(request.getFaceAuthTime());
		request1.setFaceAuthType(request.getFaceAuthType());
		payConfigClientService.updateConsDeviceInfo(request1);

		return ResultUtil.success();
	}

}
